Add net9 TimeSpan unit constants - #591
Merged
Merged
Conversation
Adds the fifteen unit constants introduced in net9:
HoursPerDay
MicrosecondsPerDay, PerHour, PerMillisecond, PerMinute, PerSecond
MillisecondsPerDay, PerHour, PerMinute, PerSecond
MinutesPerDay, PerHour
SecondsPerDay, PerHour, PerMinute
Polyfill already had the net9 TimeSpan.From* overloads that shipped
alongside these, so the feature was half covered.
In the BCL these are const fields. Extension members cannot declare
constants, so they are exposed as static properties. Reading them
compiles the same way, but they cannot be used in a constant expression,
a switch case, an attribute argument or a default parameter value. That
is recorded as a note on each one.
HoursPerDay is int, the rest are long, matching the BCL.
Beyond asserting each value, the tests cross-check them against the
TicksPer* constants that have always been in the BCL, so the numbers are
verified against something independent of this change.
This was referenced Sep 10, 2026
This was referenced Sep 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the fifteen unit constants introduced in net9:
HoursPerDayMicrosecondsPerDay/PerHour/PerMillisecond/PerMinute/PerSecondMillisecondsPerDay/PerHour/PerMinute/PerSecondMinutesPerDay/PerHourSecondsPerDay/PerHour/PerMinuteAPI count 1073 to 1088.
Polyfill already had the net9
TimeSpan.From*overloads that shipped alongside these, andTimeSpanPolyfill.cseven carried five of the values as private consts to implement them, so the feature was half covered.These are properties, not consts
In the BCL these are
constfields. Extension members cannot declare constants, so they are exposed as static properties instead.Reading them compiles identically, which covers essentially all use. What does not work is a constant expression:
Also switch cases, attribute arguments and default parameter values. There is no way around it short of the BCL type itself, so it is recorded as a
//Note:on each of the fifteen.HoursPerDayisintand the rest arelong, matching the BCL, so that assignments tointkeep compiling.Tests
Beyond asserting each value, a second test cross-checks them against the
TicksPer*constants that have been in the BCL since the beginning:So the numbers are verified against something independent of this change rather than only against literals I typed. Note that
TicksPerMicrosecondcould not be used for this, since it is itself net7 only, so the microsecond values are chained through milliseconds instead.On net9 and above these tests read the BCL consts, and below they read the polyfilled properties, with the same assertions either way.
Verification
Consumebuilds clean in Debug across all 22 target frameworks.PublicTests,EmbeddedTests,UnsafeTests,NoRefsTestsandNoExtrasTests.